/** 创建组件对象 * */
var companyForm = new Form();
var companyTable = new Table();
var companyPage = new Page();
var companyDialog = new Dialog();
var companyAjax = new Ajax();
var array = [ "companyQueryDiv", "buttonDiv" ];
var iframeIndex;
var shadeDivIndex;
var companyObj;
var keyName = "companyId";
var objName = "companyObj";
var companyList;

/** 页面加载完执行 * */
$(function() {
	initScript();
	initConditions();
	initButton();
	pageQuery();
	resetTableHeight("companyTableDiv", array);
	companyTable.reloadData();
	getSelectData();
	initElementsControl();
});

/** 初始化script * */
function initScript() {
	loadScript(appPath + "/project-yddemo/dialog/locale/ComDialog_" + appLocale
			+ ".js"); // 1.加载国际化js*
	loadScript(appPath
			+ "/project-yddemo/dialog/properties/CompanyProperties_Dialog.js"); // 2.加载配置js(非必配,配置也可写在本js中)
	loadScript(appPath + "/project-yddemo/dialog/Common.js");
}

/** 初始化查询条件 * */
function initConditions() {
	companyForm.initTableConditions({
		divId : "companyQueryDiv", // 1.归属div*
		type : "table",
		conditions : CompanyProperties_Dialog.getViewConditions(), // 2.配置条件*
		button : { // 4.操作按钮*
			width : "80", // 占用宽度*
			align : "center", // left,center,right,百分比*
			label : commonLocale.text.search, // 文本*
			action : "common:major:view",// 增加页面元素控制
			clickFun : function() { // 按钮事件*
				companyTable.reloadData(companyForm.getQueryConditions());
			}
		}
	});
	if (!$.isEmptyObject(sessionStorage.keyName)) {
		keyName = sessionStorage.keyName;
		sessionStorage.removeItem("keyName");
	}
	if (!$.isEmptyObject(sessionStorage.objName)) {
		objName = sessionStorage.objName;
		sessionStorage.removeItem("objName");
	}
	iframeIndex = window.parent.document.getElementsByTagName('iframe').length;
	shadeDivIndex = window.parent.document.getElementsByClassName("shadeDiv").length;
}

function initButton() {
	$(".formButtonDiv")
			.append(
					"<div style='width:50%;'>"
							+ "<span class='formButton' style='float:right; margin-right:10%;' onclick='closeDialog()'>"
							+ commonLocale.text.close
							+ "</span>"
							+ "</div>"
							+ "<div class='action' style='width:50%;'>"
							+ "<span class='formButton' style='float:left; margin-right:10%;' onclick='saveData()'>"
							+ commonLocale.text.okey + "</span>" + "</div>");
}

/** 分页查询 * */
function pageQuery() {
	/*companyList = companyAjax.sendAjax({
		url : requestPath + "/common/comMajors/findMajors", // 请求url*
		async : false, // 是否异步*
		type : "GET", // 请求类型*
		data : companyForm.getQueryConditions()
	// 请求参数*
	});*/
	companyList = getAllCompany();
	companyTable.initTable({
		divId : "companyTableDiv", // 1.归属div*
		datas : {
			dataList : companyList
		},
		columns : CompanyProperties_Dialog.getViewColumns(),// 3.表格列配置
		isSingle : true
	});
}

function getSelectData() {
	getCompanyObj();
	if (companyObj!=undefined&&companyObj.id!=""&&companyObj.id!=undefined) {
		var ids = [ companyObj.id ];
		companyTable.setSelectRows(ids, false);
	}
}

/** 保存数据 * */
function saveData() {
	var selectDatas = companyTable.getSelectDatas();
	companyObj = selectDatas[0];
	// 首先根据遮罩层定位层级数。其次，根据弹出框定位要返回的对象或者标签位置。
	if (shadeDivIndex == 1) {
		if (iframeIndex == 1) {
			window.parent[objName] = selectDatas[0];
			if (typeof window.parent.setMajorInput == 'function') {
				window.parent.setMajorInput();
			} else {
				setMajorInput();
			}
		} else {
			var len;
			var length = window.parent.document.getElementsByTagName("iframe")[0].parentNode.previousElementSibling.children.length;
			for (var i = 0; i < length; i++) {
				var className = window.parent.document
						.getElementsByTagName("iframe")[0].parentNode.previousElementSibling.children[i].className
						.indexOf("active");
				if (className > 0) {
					len = i;
				}
			}
			window.parent.document.getElementsByTagName("iframe")[len].contentWindow[objName] = selectDatas[0];
			if (typeof window.parent.document.getElementsByTagName("iframe")[len].contentWindow.setMajorInput == 'function') {
				var func = window.parent.document
						.getElementsByTagName("iframe")[len].contentWindow.setMajorInput;
				func();
			} else {
				setMajorInput();
			}
		}
	} else {
		if (window.parent.$(".shadeDiv")[shadeDivIndex - 2].children[0].children[1].contentWindow
				.$("iframe").length == 0) {
			window.parent.$(".shadeDiv")[shadeDivIndex - 2].children[0].children[1].contentWindow[objName] = selectDatas[0];
			if (typeof window.parent.$(".shadeDiv")[shadeDivIndex - 2].children[0].children[1].contentWindow.setMajorInput == 'function') {
				var func = window.parent.$(".shadeDiv")[shadeDivIndex - 2].children[0].children[1].contentWindow.setMajorInput;
				func();
			} else {
				setMajorInput();
			}
		} else {
			var len;
			var length = window.parent.$(".shadeDiv")[shadeDivIndex - 2].children[0].children[1].contentWindow
					.$("iframe")[0].parentNode.previousElementSibling.children.length;
			for (var i = 0; i < length; i++) {
				var className = window.parent.$(".shadeDiv")[shadeDivIndex - 2].children[0].children[1].contentWindow
						.$("iframe")[0].parentNode.previousElementSibling.children[i].className
						.indexOf("active");
				if (className > 0) {
					len = i;
				}
			}
			// window.parent.document.getElementsByTagName("iframe")[len].contentWindow[objName]
			// = selectDatas[0];
			window.parent.$(".shadeDiv")[shadeDivIndex - 2].children[0].children[1].contentWindow
					.$("iframe")[len].contentWindow[objName] = selectDatas[0];
			if (typeof window.parent.$(".shadeDiv")[shadeDivIndex - 2].children[0].children[1].contentWindow
					.$("iframe")[len].contentWindow.setMajorInput == 'function') {
				var func = window.parent.$(".shadeDiv")[shadeDivIndex - 2].children[0].children[1].contentWindow
						.$("iframe")[len].contentWindow.setMajorInput;
				func();
			} else {
				setMajorInput();
			}
		}
	}

	// 关闭弹出框
	var div = window.frameElement.parentElement;
	var shadeDiv = $(div).parent();
	$(div).remove();
	if (shadeDiv.children().length == 0) {
		shadeDiv.remove();
	}
}

function closeDialog() {
	// 关闭弹出框
	var div = window.frameElement.parentElement;
	var shadeDiv = $(div).parent();
	$(div).remove();
	if (shadeDiv.children().length == 0) {
		shadeDiv.remove();
	}
}

function getCompanyObj() {
	if (shadeDivIndex == 1) {
		if (iframeIndex == 1) {
			companyObj = window.parent[objName];
		} else {
			var len;
			var length = window.parent.document.getElementsByTagName("iframe")[0].parentNode.previousElementSibling.children.length;
			for (var i = 0; i < length; i++) {
				var className = window.parent.document
						.getElementsByTagName("iframe")[0].parentNode.previousElementSibling.children[i].className
						.indexOf("active");
				if (className > 0) {
					len = i;
				}
			}
			companyObj = window.parent.document.getElementsByTagName("iframe")[len].contentWindow[objName];
		}
	} else {
		if (window.parent.$(".shadeDiv")[shadeDivIndex - 2].children[0].children[1].contentWindow
				.$("iframe").length == 0) {
			companyObj = window.parent.$(".shadeDiv")[shadeDivIndex - 2].children[0].children[1].contentWindow[objName];
		} else {
			var len;
			var length = window.parent.$(".shadeDiv")[shadeDivIndex - 2].children[0].children[1].contentWindow
					.$("iframe")[0].parentNode.previousElementSibling.children.length;
			for (var i = 0; i < length; i++) {
				var className = window.parent.$(".shadeDiv")[shadeDivIndex - 2].children[0].children[1].contentWindow
						.$("iframe")[0].parentNode.previousElementSibling.children[i].className
						.indexOf("active");
				if (className > 0) {
					len = i;
				}
			}
			companyObj = window.parent.$(".shadeDiv")[shadeDivIndex - 2].children[0].children[1].contentWindow
					.$("iframe")[len].contentWindow[objName];
		}
	}
}

function setMajorInput() {
	if (companyObj != undefined) {
		if (shadeDivIndex == 1) {
			if (iframeIndex == 1) {
				window.parent.$("[key$='" + keyName + "']").val(companyObj.name);
			} else {
				var len;
				var length = window.parent.document
						.getElementsByTagName("iframe")[0].parentNode.previousElementSibling.children.length;
				for (var i = 0; i < length; i++) {
					var className = window.parent.document
							.getElementsByTagName("iframe")[0].parentNode.previousElementSibling.children[i].className
							.indexOf("active");
					if (className > 0) {
						len = i;
					}
				}
				window.parent.document.getElementsByTagName("iframe")[len].contentWindow
						.$("[key$='" + keyName + "']").val(companyObj.name);
			}
		} else {
			if (window.parent.$(".shadeDiv")[shadeDivIndex - 2].children[0].children[1].contentWindow
					.$("iframe").length == 0) {
				window.parent.$(".shadeDiv")[shadeDivIndex - 2].children[0].children[1].contentWindow
						.$("[key$='" + keyName + "']").val(companyObj.name);
			} else {
				var len;
				var length = window.parent.$(".shadeDiv")[shadeDivIndex - 2].children[0].children[1].contentWindow
						.$("iframe")[0].parentNode.previousElementSibling.children.length;
				for (var i = 0; i < length; i++) {
					var className = window.parent.$(".shadeDiv")[shadeDivIndex - 2].children[0].children[1].contentWindow
							.$("iframe")[0].parentNode.previousElementSibling.children[i].className
							.indexOf("active");
					if (className > 0) {
						len = i;
					}
				}
				window.parent.$(".shadeDiv")[shadeDivIndex - 2].children[0].children[1].contentWindow
						.$("iframe")[len].contentWindow.$(
						"[key$='" + keyName + "']").val(companyObj.name);
			}
		}
	} else {
		if (shadeDivIndex == 1) {
			if (iframeIndex == 1) {
				window.parent.$("[key$='" + keyName + "']").val("");
			} else {
				var len;
				var length = window.parent.document
						.getElementsByTagName("iframe")[0].parentNode.previousElementSibling.children.length;
				for (var i = 0; i < length; i++) {
					var className = window.parent.document
							.getElementsByTagName("iframe")[0].parentNode.previousElementSibling.children[i].className
							.indexOf("active");
					if (className > 0) {
						len = i;
					}
				}
				window.parent.document.getElementsByTagName("iframe")[len].contentWindow
						.$("[key$='" + keyName + "']").val("");
			}
		} else {
			if (window.parent.$(".shadeDiv")[shadeDivIndex - 2].children[0].children[1].contentWindow
					.$("iframe").length == 0) {
				window.parent.$(".shadeDiv")[shadeDivIndex - 2].children[0].children[1].contentWindow
						.$("[key$='" + keyName + "']").val("");
			} else {
				var len;
				var length = window.parent.$(".shadeDiv")[shadeDivIndex - 2].children[0].children[1].contentWindow
						.$("iframe")[0].parentNode.previousElementSibling.children.length;
				for (var i = 0; i < length; i++) {
					var className = window.parent.$(".shadeDiv")[shadeDivIndex - 2].children[0].children[1].contentWindow
							.$("iframe")[0].parentNode.previousElementSibling.children[i].className
							.indexOf("active");
					if (className > 0) {
						len = i;
					}
				}
				window.parent.$(".shadeDiv")[shadeDivIndex - 2].children[0].children[1].contentWindow
						.$("iframe")[len].contentWindow.$(
						"[key$='" + keyName + "']").val("");
			}
		}
	}
}

/**
 * 窗口大小改变时触发
 * 
 * @param
 * @return
 */
$(window).resize(function() {
	resetTableHeight("companyTableDiv", array);
});
